﻿CREATE TABLE MedVerordDarForm (
	`Oid` bigint(19) NOT NULL AUTO_INCREMENT,
	`Tid` int(10) DEFAULT NULL,
	`IsActive` tinyint(1) DEFAULT NULL,
	`Notice` varchar(1024) DEFAULT NULL,
	`InsTs` datetime DEFAULT NULL,
	`InsUser` varchar(256) DEFAULT NULL,
	`Version` bigint(19) DEFAULT NULL,
	`UdpUser` varchar(256) DEFAULT NULL,
	`SystemEntryID` int(10) DEFAULT NULL,
	`Bezeichnung` varchar(256) DEFAULT NULL,
	`Wert` int(10) DEFAULT NULL,
	CONSTRAINT UCWERT UNIQUE (`Wert`),
	PRIMARY KEY (`Oid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin2;

INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Tabl.', 0);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Drag.', 1);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Trpf.', 2);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Kps.', 3);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'ml', 4);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Spray', 5);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Becher', 6);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'Zäpf.', 7);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'i.m.', 8);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'i.v.', 9);
INSERT INTO medverorddarform(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(99, 1, now(), 'System', 1 , 'System', 'subkutan', 10);

ALTER TABLE medikamentenverordnung ADD COLUMN DarreichungsformOid BIGINT(19) DEFAULT NULL AFTER MedArtOid;
ALTER TABLE medikamentenverordnung ADD CONSTRAINT FK_DARFORM2MEDVERORDNUNG FOREIGN KEY (DarreichungsformOid) REFERENCES medverorddarform(Oid)  ON DELETE NO ACTION ON UPDATE NO ACTION;

UPDATE medikamentenverordnung AS mv SET DarreichungsformOid = 
	(SELECT dfoid FROM 
		(SELECT medverorddarform.Oid as dfoid, medverorddarform.Wert AS dfwert 
			FROM medverorddarform 
			JOIN medikamentenverordnung ON medverorddarform.Wert = medikamentenverordnung.Darreichungsform) AS x WHERE x.dfwert = mv.Darreichungsform LIMIT 1) 
WHERE DarreichungsformOid IS NULL;


CREATE TABLE MedVerordDepotRhythmus (
	`Oid` bigint(19) NOT NULL AUTO_INCREMENT,
	`Tid` int(10) DEFAULT NULL,
	`IsActive` tinyint(1) DEFAULT NULL,
	`Notice` varchar(1024) DEFAULT NULL,
	`InsTs` datetime DEFAULT NULL,
	`InsUser` varchar(256) DEFAULT NULL,
	`Version` bigint(19) DEFAULT NULL,
	`UdpUser` varchar(256) DEFAULT NULL,
	`SystemEntryID` int(10) DEFAULT NULL,
	`Bezeichnung` varchar(256) DEFAULT NULL,
	`Wert` int(10) DEFAULT NULL,
	CONSTRAINT UCWERT UNIQUE (`Wert`),
	PRIMARY KEY (`Oid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin2;

INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', '', 0);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', 'wöchentlich', 1);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', '2-wöchentlich', 2);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', 'monatlich', 3);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', '3-monatlich', 4);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', 'halbjährlich', 5);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', 'jährlich', 6);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', 'täglich', 7);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', '6-wöchentlich', 8);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', '2tägig', 9);
INSERT INTO medverorddepotrhythmus(tid, isactive, insts, insuser, version, udpuser, bezeichnung, wert) VALUES(100, 1, now(), 'System', 1 , 'System', '3-wöchentlich', 10);

ALTER TABLE medikamentenverordnung ADD COLUMN DepotRhythmusOid BIGINT(19) DEFAULT NULL AFTER MedArtOid;
ALTER TABLE medikamentenverordnung ADD CONSTRAINT FK_DEPRHYTHM2MEDVERORDNUNG FOREIGN KEY (DepotRhythmusOid) REFERENCES medverorddepotrhythmus(Oid)  ON DELETE NO ACTION ON UPDATE NO ACTION;

UPDATE medikamentenverordnung AS mv SET DepotRhythmusOid = 
	(SELECT dfoid FROM 
		(SELECT medverorddepotrhythmus.Oid as dfoid, medverorddepotrhythmus.Wert AS dfwert 
			FROM medverorddepotrhythmus 
			JOIN medikamentenverordnung ON medverorddepotrhythmus.Wert = medikamentenverordnung.Depotrhythmus) AS x WHERE x.dfwert = mv.Depotrhythmus LIMIT 1) 
WHERE DepotRhythmusOid IS NULL;